/**
 * 
 */
package com.cua24.donkeyroll;

import org.andengine.engine.Engine;
import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.camera.Camera;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.adt.color.Color;
import org.andengine.util.debug.Debug;

/**
 * @author dikaberg
 *
 */
public class ResourcesManager {
		
	private static final ResourcesManager INSTANCE = new ResourcesManager();
	public Engine engine;
	public GameActivity activity;
	public VertexBufferObjectManager vbom;
	public BoundCamera camera;
	public ITextureRegion splash_region;
    private BitmapTextureAtlas splashTextureAtlas;
    
    public ITextureRegion menu_background_region;
    public ITextureRegion play_region;
    public ITextureRegion options_region;
    public Font font;
    
    private BuildableBitmapTextureAtlas menuTextureAtlas;
	
    private void loadMenuFonts(){//*//
    	FontFactory.setAssetBasePath("font/");
    	final ITexture mainFontTexture = new BitmapTextureAtlas(activity.getTextureManager(),256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);	
    	font = FontFactory.createStrokeFromAsset(activity.getFontManager(), mainFontTexture, activity.getAssets(), "leadcoat.ttf", 50, true, Color.WHITE_ARGB_PACKED_INT, 2, Color.BLACK_ARGB_PACKED_INT);
    	font.load();
    }
	public void loadMenuResources(){//*//
		loadMenuGraphics();
		loadMenuAudio();
		loadMenuFonts();
	}
	
	public void loadGameResources(){//*//
		loadGameGraphics();
		loadGameFonts();
		loadGameAudio();
	}
	
	private void loadMenuGraphics(){//*//
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/menu/");
        menuTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR);
        menu_background_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "menu_background.png");
        play_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "play.png");
        options_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, activity, "options.png");
    	try{
			this.menuTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0));
			this.menuTextureAtlas.load();
		} 
    	catch (final TextureAtlasBuilderException e){
			Debug.e(e);
		}
	}
    
    private void loadMenuAudio(){//*//
        
    }

    private void loadGameGraphics(){//*//
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/");
        gameTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(),1024,1024,TextureOptions.BILINEAR);
        platform1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform1.png");
        platform2_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform2.png");
        platform3_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform3.png");
        coin_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "coin.png");
        player_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas, activity, "player.png", 3, 1);
        complete_window_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "levelCompleteWindow.png");
        complete_star_region = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(gameTextureAtlas, activity, "star.png",2,1);
        try{
        	this.gameTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource,BitmapTextureAtlas>(0,1,0));
        	this.gameTextureAtlas.load();
        }catch(final TextureAtlasBuilderException e){
        	Debug.e(e);
        }
        
    }
    
    private void loadGameFonts(){//*//
        
    }
    
    private void loadGameAudio(){//*//
        
    }
    
    public void loadSplashScreen(){//*//
    	BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
    	splashTextureAtlas = new BitmapTextureAtlas(activity.getTextureManager(),256,256,TextureOptions.BILINEAR);
    	splash_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, activity, "splash.png", 0, 0);
    	splashTextureAtlas.load();
    }
    
    public void unloadSplashScreen(){//*//
    	splashTextureAtlas.unload();
    	splash_region = null;
    }
	
    public static void prepareManager(Engine engine,GameActivity activity,BoundCamera camera,VertexBufferObjectManager vbom){
    	getInstance().engine = engine;
    	getInstance().activity = activity;
    	getInstance().camera = camera;
    	getInstance().vbom = vbom;
    }
    
    public static ResourcesManager getInstance(){//*//
    	return INSTANCE;
    }
    
    public void unloadMenuTextures(){//*//
    	menuTextureAtlas.unload();
    }
    public void loadMenuTextures(){//*//
    	menuTextureAtlas.load();
    }
    
    public void unloadGameTextures(){//*//
    	
    }
    
    public BuildableBitmapTextureAtlas gameTextureAtlas;
    
    public ITextureRegion platform1_region;
    public ITextureRegion platform2_region;
    public ITextureRegion platform3_region;
    public ITextureRegion coin_region;
    
    public ITiledTextureRegion player_region;
    public ITextureRegion complete_window_region;
    public ITiledTextureRegion complete_star_region;
}
